<?php

/////////
//协议谈判文件生成上传
////////
/*
function xieyitanpan_file_create_page(){
	 drupal_set_title('协议谈判文件生成');
	 
	 $render_array = array(
	   		'#markup' => '',
     );
	 
     $form = drupal_get_form('xieyitanpan_file_create_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 return $render_array;
}

function xieyitanpan_file_create_form($form, &$form_state){
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['upload'] = array(
	'#type' => 'managed_file',
	'#title' => '协议谈判文件上传',
	'#required' => TRUE,
	'#upload_location' => 'public://my_uploads',
	'#upload_validators' => array(
	    'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(20*1024*1024)),
	);
	
	 $form['file_name'] = array(
	  '#title' => t('显示文件名：'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>"",
	 );	 
	 
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>"",
	 );
	 	  	 
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('确认'),
	 );
	  return $form;
}

function xieyitanpan_file_create_form_submit($form, &$form_state){
	$values=$form_state['values'];	
	$fid = $form_state['values']['upload']; 
	$filename = '';
	$file = file_load($fid);  
	$saveurl = file_uri_target($file->uri);
	$fid = $file->fid;
	global $user; 
	if ($file != null) {   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);			
				$last_id = db_insert('xieyitanpan_file_info')
			  		->fields(array(
			   			 'name' => $values['file_name'],
			   			 'url' => $saveurl,
			   			 'timestamp' => $_SERVER['REQUEST_TIME'],
			   			 'upload_name' => $values['upload_name'],
			   			 'remark' => $values['remark'],
			   			 'user_id' => $user->uid,
			   			 'file_managed_id' => $fid,
			  			))->execute();
				  if ($last_id!=NULL) {			
					 $form_state['redirect'] = 'xieyitanpan_file/list';					 	    
				  } else {
				    
				    // 数据写入失败了
				    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
				    
				  }
		} 
		else {
		form_set_error('upload',$extension_errors[0]); 
		}
	}
}
*/
function xieyitanpan_file_create_fund_page($fund_id){
	 drupal_set_title('协议谈判文件');
	 $page_content = '';
	 $page_content .= l('返回列表','xieyitanpan_file/fund') .  '<br />'. '<br />';	
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
     $fund_info = get_info_by_id($fund_id);
     $funds_name = l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']);
	  
	 $page_content .= '<table style="width:700px"><tbody><tr><th colspan="3">'.$funds_name.' : 协议谈判文件列表</tr>';
	 $page_content .= '<tr><td width=\'55%\'>文件名称</td><td width=\'30%\'>上传时间</td><td width=\'15%\'>操作</td></tr>';
     $count = 0;
     $files_array =  array();
     $query = "SELECT id, name, url, timestamp FROM " .
           "{xieyitanpan_file_info} where fund_id = ?";
	 $queryResult = db_query($query, array($fund_id));
     foreach ($queryResult as $links ) {
	  	 $datetime = format_date($links->timestamp, 'custom', 'Y-m-d');  
	     $count += 1;
	  	 $page_content .='<tr>';
		 $page_content .= '<td>'. l($links->name, 'http://' .$_SERVER['HTTP_HOST'].'/drupal/sites/default/files/'.$links->url). '</td>';		  
		 $page_content .= '<td>'. $datetime.'</td>';
		 $page_content .= '<td>'. l('编辑','xieyitanpan_file/edit/'.$links->id).'</td>';
		  $page_content .='</tr>';
	 }
	 if($count == 0)
	  	$page_content .= '<tr><th colspan="4">无附件</tr>';
	 $page_content .= '</tbody></table></br>';
	 
	 $render_array = array(
	   		'#markup' => $page_content,
     );
	 
     $form = drupal_get_form('xieyitanpan_file_create_fund_form',$fund_id);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 return $render_array;
}

function fundcreate_fundid_page($fund_id){
	 drupal_set_title('开通基金管理账号');
	 $page_content = "确定开通该账户的基金管理账户权限?";
	 $_SESSION['fundcreate_fundid'] = $fund_id;
	  $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('fundcreate_fundid_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;
	 
}

function fundcreate_fundid_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('fundcreate_fundid_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('确认开通'),
  );
  
  return $form;
}

function fundcreate_fundid_form_back($form, &$form_state){
	if(empty($_SESSION['fundcreate_fundid'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'xieyitanpan_file/fund';
	  }else{
		  $values = $_SESSION['fundcreate_fundid'];
		  unset($_SESSION['fundcreate_fundid']);
		  $form_state['redirect'] = 'create_fund/fundid/'.$values;
	  }
}

function fundcreate_fundid_form_submit($form, &$form_state){
      //简单的重定向到contactus页面
      if(empty($_SESSION['fundcreate_fundid'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'xieyitanpan_file/fund';
	  }else{
	  	  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
		  $values = $_SESSION['fundcreate_fundid'];
		  unset($_SESSION['fundcreate_fundid']);
		  set_state_by_id(13,$values);
		  $fund_info = get_info_by_id($values);
		  $last_id = db_insert('fund_basic_information')
			  		->fields(array(
			   			 'user_id' => $fund_info['user_id'],
			   			 'fund_id' => $fund_info['id'],
			   			 'registered_name' => $fund_info['nishejijinmingcheng'],
			  			))->execute();
		  if ($last_id!=NULL) {			
			 $form_state['redirect'] = 'xieyitanpan_file/fund';					 	    
		  } else {
		    // 数据写入失败了
			 drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
		  }
	  }

}

function xieyitanpan_file_create_fund_form($form, &$form_state,$fund_id){
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['bid'] = array('#type' => 'hidden', '#value' => $fund_id);
	 $form['upload'] = array(
	'#type' => 'managed_file',
	'#title' => '上传新文件',
	'#required' => TRUE,
	'#upload_location' => 'public://my_uploads',
	'#upload_validators' => array(
	    'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(20*1024*1024)),
	);
	
	 $form['file_name'] = array(
	  '#title' => t('显示文件名：'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>"",
	 );	 

	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>"",
	 );
	 	  	 
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('确认'),
	 );
	  return $form;
}

function xieyitanpan_file_create_fund_form_submit($form, &$form_state){
	$values=$form_state['values'];	
	$fid = $form_state['values']['upload']; 
	$filename = '';
	$file = file_load($fid);  
	$saveurl = file_uri_target($file->uri);
	$fid = $file->fid;
	if ($file != null) {   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);			
				$last_id = db_insert('xieyitanpan_file_info')
			  		->fields(array(
			   			 'name' => $values['file_name'],
			   			 'url' => $saveurl,
			   			 'timestamp' => $_SERVER['REQUEST_TIME'],
			   			 'remark' => $values['remark'],
			   			 'file_managed_id' => $fid,
			   			 'fund_id' => $values['bid'],
			  			))->execute();
				  if ($last_id!=NULL) {			
					 $form_state['redirect'] = 'xieyitanpan_file/create/fundid/'.$values['bid'];					 	    
				  } else {
				    
				    // 数据写入失败了
				    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
				    
				  }
		} 
		else {
		form_set_error('upload',$extension_errors[0]); 
		}
	}
}
/*
//协议谈判文件生成浏览
function xieyitanpan_file_view_page($perid) {
  // content variable that will be returned for display
  $page_content = '';
  
  global $user;
   if(user_access('upload edit files')){
 		$page_content .= l('编辑','xieyitanpan_file/edit/'.$perid) .  '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
  }
  $page_content .= l('返回列表','xieyitanpan_file/list') . '<br />'. '<br />';
  $query = "SELECT id, name,url,timestamp,remark,user_id,upload_name FROM " .
           "{xieyitanpan_file_info} where id = ?";
  $queryResult = db_query($query, array($perid));
  $ifempty = TRUE;
 
  foreach($queryResult as $links){
   	  $ifempty = FALSE;
	  $page_content .= '<h5>协议谈判文件材料下载:</h5>'.l($links->name, 'http://' .$_SERVER['HTTP_HOST'].'/drupal/sites/default/files/'.$links->url).'<br />'.'<br />';
	  $page_content .= '<h5>上传人员:</h5>'.$links->upload_name.'<br />'.'<br />';
	  $page_content .= '<h5>上传时间: </h5>'. format_date($links->timestamp, 'custom', 'Y-m-d').'<br />'.'<br />';
	  $page_content .= '<h5>备注:</h5>'.$links->remark.'<br />'.'<br />';
  }	
  // More coming....
  if ($ifempty) {
    $page_content = "没有该协议谈判文件材料信息.";
  }
  return $page_content;
}
*/
//协议谈判文件生成编辑
function xieyitanpan_file_edit_page($perid) {	
  $page_content = '';
  drupal_set_title('协议谈判文件管理');
	 
  $query = "SELECT id,name, url, fund_id FROM " .
           "{xieyitanpan_file_info} where id = ?";
  $queryResult = db_query($query, array($perid));
  $ifempty =TRUE;
  foreach ($queryResult as $links ) {
	  if($links->id>0)
	  {
	  		$page_content .= l('返回列表','xieyitanpan_file/create/fundid/'.$links->fund_id) .  '<br />'. '<br />';	
			$ifempty = FALSE;
	  }	  
  }
  if($ifempty)
	 {
	 	drupal_goto('xieyitanpan_file/create/fundid/'.$links->id);
	 }
 else{
	 	 $page_content .= '<h5>已上传协议谈判文件:</h5>'.l($links->name, 'http://' .$_SERVER['HTTP_HOST'].'/drupal/sites/default/files/'.$links->url).'<br />'.'<br />';
	     $render_array = array(
	   		'#markup' => $page_content,
		 );
	 
		$form = drupal_get_form('xieyitanpan_file_edit_form',$perid);
  		$render_array['#markup'] .= drupal_render($form);
	 }	 
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function xieyitanpan_file_edit_form($form, &$form_state, $perid){
  
  $query = "SELECT id, name,url,remark, file_managed_id,fund_id FROM " .
           "{xieyitanpan_file_info} where id = ?";
  $queryResult = db_query($query, array($perid));
 
  $name = '';
  $url = '';
  $remark = ''; 
  $fund_id = '';
 foreach ($queryResult as $links ) {
    $name =  $links->name;
	$url =  $links->url;
    $remark =  $links->remark;
	$file_managed_id = $links->file_managed_id;
	$fund_id = $links->fund_id;
  }
   	$form['bid'] = array('#type' => 'hidden', '#value' => $perid);
	$form['file_managed_id'] = array('#type' => 'hidden', '#value' => $file_managed_id);
	$form['fund_id'] = array('#type' => 'hidden', '#value' => $fund_id);	
	$form['name'] = array(
	  '#title' => t('文件名'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['xieyitanpan_file_edit_form']['name'])?$_SESSION['xieyitanpan_file_edit_form']['name']:$name,
	 );
	 
	 
	 $form['upload'] = array(
	  '#type' => 'managed_file',
	  '#title' => '重新提交协议谈判文件材料',
	  '#required' => FAlSE,
	  '#upload_location' => 'public://my_uploads',
	  '#upload_validators' => array(
	    'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(20*1024*1024)),
	);

	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['xieyitanpan_file_edit_form']['remark'])?$_SESSION['xieyitanpan_file_edit_form']['remark']:$remark,
	 );
	 
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交修改'),
	  );
	 //表单元素“提交”按钮
	  $form['deleate'] = array(
	    '#type' => 'submit',
	    '#value' => t('删除该材料'),
	    '#submit' => array('xieyitanpan_file_edit_form_delete'),
	  );
		  
  return $form;
}

function xieyitanpan_file_edit_form_submit($form, &$form_state){
	$values=$form_state['values'];	
	$fid = $form_state['values']['upload']; 
	$filename = '';
	$file = file_load($fid);  

	global $user; 
	$filename = $values['name'];
	$filemanagedid = $values['file_managed_id'];
	$uploadid = $values['bid'];
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
		$fid = $file->fid;   
		//$extension_errors=file_validate_extensions($file, "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp docx");   
	    $extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
								
			if($filemanagedid>0){
				$file = file_load($filemanagedid);
				  if($file!=NULL){
					 file_delete($file);
				  }
				db_delete('file_managed')
				  ->condition('fid',$filemanagedid)
				  ->execute();
			}	
			db_update('xieyitanpan_file_info')
			  	 ->fields(array(
			   		'name' => $filename,
			   		'url' => $saveurl,
			   		'timestamp' => $_SERVER['REQUEST_TIME'],
			   	    'remark' => $values['remark'],
			   	    'file_managed_id' => $fid,
			  		))
			  		->condition('id',$uploadid)
			  		->execute();				
					
			$form_state['redirect'] =  'xieyitanpan_file/create/fundid/'.$values['fund_id'];
		}			
		else {
			form_set_error('upload',$extension_errors[0]); 
		}
	}
	else{
		db_update('xieyitanpan_file_info')
			 ->fields(array(
			   	'name' => $filename,
			   	'timestamp' => $_SERVER['REQUEST_TIME'],
			   	'remark' => $values['remark'],			   	
			   	'upload_name' => $values['upload_name'],
			  ))
			  ->condition('id',$uploadid)
			  ->execute();				
				
	    $form_state['redirect'] = 'xieyitanpan_file/create/fundid/'.$values['fund_id'];
	}
}

function xieyitanpan_file_edit_form_delete($form, &$form_state){
	 $values = NULL;
	 $_SESSION['xieyitanpan_file_edit_form'] = $form_state['values'];
	 $values = $_SESSION['xieyitanpan_file_edit_form'] ;
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'xieyitanpan_file/delete/'.$values['bid'];
}

//协议谈判文件生成删除
function xieyitanpan_file_delete_page($perid) {
  // content variable that will be returned for display
  $page_content = '删除后该内容将不可恢复！';
  
  drupal_set_title('删除协议谈判文件内容');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('xieyitanpan_file_delete_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function xieyitanpan_file_delete_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('xieyitanpan_file_delete_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
  );
  
  return $form;
}

function xieyitanpan_file_delete_form_back($form, &$form_state){
	if(empty($_SESSION['xieyitanpan_file_edit_form'])){
		  	$form_state['redirect'] = 'xieyitanpan_file/create/fundid/'.$values['fund_id'];
	  }else{
		  $values = $_SESSION['xieyitanpan_file_edit_form'];
		  unset($_SESSION['xieyitanpan_file_edit_form']);
		  $form_state['redirect'] = 'xieyitanpan_file/create/fundid/'.$values['fund_id'];
	  }
}

function xieyitanpan_file_delete_form_submit($form, &$form_state){
 if(empty($_SESSION['xieyitanpan_file_edit_form'])){
		  $form_state['redirect'] = 'xieyitanpan_file/list';
	  }else{
		  $values = $_SESSION['xieyitanpan_file_edit_form'];
		  unset($_SESSION['xieyitanpan_file_edit_form']); 
		  $file = file_load($values['file_managed_id']);
				  if($file!=NULL){
					 file_delete($file);
				  }
		  db_delete('file_managed')
				  ->condition('fid',$values['file_managed_id'])
				  ->execute();
		  db_delete('xieyitanpan_file_info')
			->condition('id' ,$values['bid'])
			->execute();
			
		  $form_state['redirect'] = 'xieyitanpan_file/create/fundid/'.$values['fund_id'];

	  }

}

function xieyitanpan_file_fundlist_page()
{
  module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
  $pici_id = get_pici_id_bystate(6);
  $query = "SELECT endtimestamp FROM " .
           "{enterprise_announce} WHERE pici_id = ?";
  $queryResult = db_query($query, array($pici_id));
  $enddatetime = '';
  $endtimetamp = '';
  foreach ($queryResult as $links ) {
  	$enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	$endtimetamp = $links->endtimestamp;
  }
  $page_content = '';
  $page_content .= '<table style="width:850px"><tbody>';
  $page_content .='<tr><td width=\'40%\'>基金名称</td><td width=\'15%\'>谈判开始时间</td><td width=\'15%\'>剩余谈判时间(天)</td><td width=\'15%\'>附件管理</td><td width=\'15%\'>操作</td></tr>';
      
  $count = 0;
  $today = getdate();
  $fundlist_array = get_part_by_state(11);
  $todaytime =  mktime(0, 0, 0,$today['mon'], $today['mday'], $today['year']);
  $tanpanjiesu = (180*24*3600 - ($todaytime-$endtimetamp))/(24*3600);
  	foreach ($fundlist_array as $fund ) {
  	  $count += 1;
  	  $page_content .='<tr>';
	  $page_content .= '<td>'. l($fund['nishejijinmingcheng'], 'enterprise_information2/viewbyid/'.$fund['id']). '</td>';		   
	  $page_content .= '<td>'. $enddatetime.'</td>';
	  $page_content .= '<td>'. $tanpanjiesu.'</td>';
      $page_content .= '<td>'. l('附件管理', 'xieyitanpan_file/create/fundid/'.$fund['id']).'</td>';
	  $page_content .= '<td>'. l('开通账号', 'create_fund/fundid/'.$fund['id']).'</td>';
	  $page_content .='</tr>';
    }
  
  
  if($count == 0)
  	$page_content .= '<tr><th colspan="5">此阶段无基金信息</tr>';
  $page_content .= '</tbody></table></br>';
	return $page_content;
}

function xieyitanpan_file_history_page(){
	 module_load_include("inc", "enterprise_information2","enterprise_information2.pages");
  module_load_include("inc", "expert_pici_manage","expert_pici_manage.pages");
  $pici_id = get_pici_id_bystate(6);
  $query = "SELECT endtimestamp FROM " .
           "{enterprise_announce} WHERE pici_id = ?";
  $queryResult = db_query($query, array($pici_id));
  $enddatetime = '';
  $endtimetamp = '';
  foreach ($queryResult as $links ) {
  	  $enddatetime = format_date($links->endtimestamp, 'custom', 'Y-m-d'); 
	  $endtimetamp = $links->endtimestamp;
  }
  $page_content = '';
  $page_content .= '<table style="width:700px"><tbody>';
  $page_content .='<tr><td width=\'45%\'>基金名称</td><td width=\'20%\'>谈判开始时间</td><td width=\'20%\'>谈判结束时间</td><td width=\'15%\'>附件管理</td></tr>';
      
  $count = 0;
  $today = getdate();
  $fundlist_array = get_part_by_state(12);
  $todaytime =  mktime(0, 0, 0,$today['mon'], $today['mday'], $today['year']);
  $tanpanjiesu = (180*24*3600 - ($todaytime-$endtimetamp))/(24*3600);
  foreach ($fundlist_array as $fund ) {
  	  $count += 1;
  	  $page_content .='<tr>';
	  $page_content .= '<td>'. l($fund['nishejijinmingcheng'], 'enterprise_information2/viewbyid/'.$fund['id']). '</td>';		   
	  $page_content .= '<td>'. $enddatetime.'</td>';
	  $page_content .= '<td>'. $tanpanjiesu.'</td>';
      $page_content .= '<td>'. l('附件管理', 'xieyitanpan_file/create/fundid/'.$fund['id']).'</td>';
	  $page_content .='</tr>';
  }
  if($count == 0)
  	$page_content .= '<tr><th colspan="4">无历史基金信息</tr>';
  $page_content .= '</tbody></table></br>';
	return $page_content;
}
/*
//协议谈判文件生成列表
function xieyitanpan_file_list_page() {
  // content variable that will be returned for display
  $html = '';
  $html .=  l('返回列表', 'xieyitanpan_file/fund').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  $html .=  l('上传协议谈判文件', 'xieyitanpan_file/create').'<br><br>';
  $header = array(
      array('data' => '已上传协议谈判文件材料', 'field' => 'name'),
      array('data' => '上传人员', 'field' => 'upload_name'),
      array('data' => '上传时间', 'field' => 'timestamp', 'sort' => 'desc'),
      array('data' => '备注', 'field' => 'remark'),
      array('data' => '编辑', 'field' => 'edit'),
      array('data' => '删除', 'field' => 'deleate'),
      );
  $query = db_select('xieyitanpan_file_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(10)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'url',
      'timestamp',
      'remark',
      'user_id',
      'upload_name',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
      $rows[] = array(
      'data' => array(
        l($node->name, 'xieyitanpan_file/view/'. $node->id),
        $node->upload_name,
        format_date($node->timestamp, 'custom', 'Y-m-d'),
        $node->remark,
        l('编辑','xieyitanpan_file/edit/'. $node->id),
        l('删除','xieyitanpan_file/delete/'. $node->id),
      )
      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有上传记录',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}
//协议谈判文件列表
function xieyitanpan_file_fundlist_perpage($fund_id) {
  // content variable that will be returned for display
  $html = '';
  $html .=  l('返回基金选择列表', 'xieyitanpan_file/fund'). '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  $html .=  l('上传协议谈判文件', 'xieyitanpan_file/create/fundid/'.$fund_id).'<br><br>';
  $query = "SELECT id, name FROM " .
           "{guiding_fund_info} where id = ?";
  $queryResult = db_query($query, array($fund_id));
  foreach($queryResult as $result){
  	$html .= '当前基金：' . '&nbsp;&nbsp;'. $result->name.'<br><br>';
  }
  $header = array(
      array('data' => '已上传材料', 'field' => 'name'),
      array('data' => '上传人员', 'field' => 'upload_name'),
      array('data' => '上传时间', 'field' => 'timestamp', 'sort' => 'desc'),
      array('data' => '备注', 'field' => 'remark'),
      array('data' => '编辑', 'field' => 'edit'),
      array('data' => '删除', 'field' => 'deleate'),
      );
  $query = db_select('xieyitanpan_file_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(10)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'url',
      'timestamp',
      'remark',
      'user_id',
      'upload_name',
      ))
	  ->condition('fund_id',$fund_id,'=');
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
      $rows[] = array(
      'data' => array(
        l($node->name, 'xieyitanpan_file/view/'. $node->id),
        $node->upload_name,
        format_date($node->timestamp, 'custom', 'Y-m-d'),
        $node->remark,
        l('编辑','xieyitanpan_file/edit/'. $node->id),
        l('删除','xieyitanpan_file/delete/'. $node->id),
      )
      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有上传记录',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}

//出资人设置基金选择入口
function xieyitanpan_file_fundlist_page() {
 $html = '';
 $html .= l('其他协议谈判文件列表', 'xieyitanpan_file/list').'<br><br>';
 $header = array(
      array('data' => '基金名称', 'field' => 'name', 'sort' => 'desc'),
      array('data' => '负责人', 'field' => 'principal_name'),
      array('data' => '成立时间', 'field' => 'timestamp'),
      array('data' => '协议谈判文件管理', 'field' => 'shixiang_set'),
      array('data' => '上传协议谈判文件', 'field' => 'shixiang_upload'),
      );
  $query = db_select('guiding_fund_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'principal_name',
      'timestamp',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
          $title = l('协议谈判文件管理', 'xieyitanpan_file/fundlist/'.$node->id);
		  $rows[] = array(
	      'data' => array(
	        l($node->name, 'guidingfund/list/'. $node->id),
	        $node->principal_name,
	        format_date($node->timestamp, 'custom', 'Y-m-d'),
	        $title,
	        l('新建协议谈判文件', 'xieyitanpan_file/create/fundid/'.$node->id),
	      )
	      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有基金信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}
*/